package parseAndHandleTool;

import com.oocl.crm_system.Constant;
import com.oocl.crm_system.Customer;
import com.oocl.crm_system.DoublyList;
import com.oocl.crm_system.Node;

public class CopyOfParseCmd {			//parse command which come form console
	public void parseCmd(DoublyList<Customer> list,String inputByConsole){
		String input = inputByConsole;
		switch(inputByConsole.charAt(0)){
		case 'A':
			parseCmdForAdd(list,input);
			break;
		case 'D':
			parseCmdForDelete(list,input);
			break;
		case 'U':
			parseCmdForUpdate(list, input);
			break;
		case 'O':
			parseCmdForSort(list,input);
			break;
		case 'H':
			parseCmdForHelp(list, input);
			break;
		case 'L':
			parseCmdForLook(list, input);
			break;
		default:
			System.out.println("it is a wrong command,please enter a correct command.");
			break;
		}
	}
	
	/**
	 * 
	 * @param list
	 * @param inputByConsole
	 */
	public void parseCmdForAdd(DoublyList<Customer> list,String inputByConsole){
		String input = inputByConsole;
		input = input.substring(2);
		
		String[] buf = input.split(",");
		String[] buf1 = new String[2];
		String[] buf2 = new String[buf.length];          //get the value from input 
		
		for(int i = 0; i < buf.length; i++){
			buf1 = buf[i].split(":");
			buf2[i] = buf1[1];
		}
		
		//change id = '123' to id = 123
		Customer customer = new Customer(Integer.parseInt(buf2[0]),buf2[1],buf2[2],buf2[3],buf2[4],buf2[5]);
		list.insertTail(customer);
	}
	
	/**
	 * 
	 * @param list
	 * @param inputByConsole
	 * @return void
	 * @Description delete the customer from system
	 */
	public void parseCmdForDelete(DoublyList<Customer> list,String inputByConsole){
		String input = inputByConsole;
		input = input.substring(2);
		
		int id = Integer.parseInt(input);
		
		for(int i = 0; i < list.nodeNum; i++){
			Node<Customer> delNode = list.findNodeByIndex(i);
			if((delNode.value).getId() == id)
				list.delNode(delNode.value);            //delete the customer who's id is 123
		}
	}
	
	/**
	 * 
	 * @param list
	 * @param inputByConsole
	 * @return void
	 * @Description update customer's message by id
	 */
	public void parseCmdForUpdate(DoublyList<Customer> list,String inputByConsole){
		String input = inputByConsole;
		
		String[] str = input.split(" ");
		int id = Integer.parseInt(str[1]);         //get the customer's id
		System.out.println("customer's id is ====================" + id);
		input = str[2];

		//handle filed which you want to update
		String[] buf = input.split(",");
		String[] buf1 = new String[2];
		String[] buf2 = new String[buf.length];
		String[] buf3 = new String[buf.length];
		
		for(int i = 0; i < buf.length; i++){
			buf1 = buf[i].split(":");
			buf2[i] = buf1[1];
			buf3[i] = buf1[0];
			System.out.println(buf2[i] + ":" + buf3[i]);
		}
		
		for(int i = 0; i < list.nodeNum; i++){
			Node theNode = list.findNodeByIndex(i);
			Customer thecustomer = (Customer) theNode.value;
			
			if(thecustomer.getId() == id){
				
				list.delNode(thecustomer);			//delete the customer first
				
				for(int j = 0; j < buf.length; j++){
					if(buf3[j].equals("name")){
						thecustomer.setName(buf2[j]);
					}
					else if(buf3[j].equals("sex")){
						thecustomer.setSex(buf2[j]);
					}
					else if(buf3[j].equals("birthday")){
						thecustomer.setBirthday(buf2[j]);
					}
					else if(buf3[j].equals("address")){
						thecustomer.setAddress(buf2[j]);
					}
					else if(buf3[j].equals("call")){
						thecustomer.setCall(buf2[j]);
					}
					else if(buf3[j].equals("id")){
						thecustomer.setId(Integer.parseInt(buf2[j]));
					}
					else{
						
					}
				}
				
				list.insertTail(thecustomer);			//add the customer back to doublyList second
			}
		}		
	}
	
	/**
	 * 
	 * @param list
	 * @param inputByConsole
	 * @return void
	 * @Description look for customer by name or id, sex, birthday, address,call
	 */
	public void parseCmdForLook(DoublyList<Customer> list,String inputByConsole){
		String input = inputByConsole;
		
		if(input.length() == 1){
			list.printList();
			return;
		}
		String[] buf = input.split(" ");
		String buf2 = buf[1];        
		String buf3[] = buf2.split(":");
		String buf1 = buf3[1];
		String tt = buf3[0];
		boolean isFind = false;	
		for(int i = 0; i < list.nodeNum; i++){
			Node<Customer> currentNode = list.findNodeByIndex(i);
			Customer currentcustomer = currentNode.value;
			if(tt.equals("name")){
				if(buf1.equals(currentcustomer.getName())){
					printAllcustomer(currentcustomer);
					isFind = true;
				}
			}
			else if(tt.equals("sex")){
				if(buf1.equals(currentcustomer.getSex())){
					printAllcustomer(currentcustomer);
					isFind = true;
				}
			}
			else if(tt.equals("birthday")){
				if(buf1.equals(currentcustomer.getBirthday())){
					printAllcustomer(currentcustomer);
					isFind = true;
				}
			}
			else if(tt.equals("address")){
				if(buf1.equals(currentcustomer.getAddress())){
					printAllcustomer(currentcustomer);
					isFind = true;
				}
			}
			else if(tt.equals("id")){
				if(Integer.parseInt(buf1) == currentcustomer.getId()){    //parse int
					printAllcustomer(currentcustomer);
					isFind = true;
				}
			}
			else if(tt.equals("call")){
				if(buf1.equals(currentcustomer.getCall())){
					printAllcustomer(currentcustomer);
					isFind = true;
				}
			}
			else{
				
			}	
		}
		
		if(!isFind){
			System.out.println("can't not find persons with this charater");
		}
	}
	
	/**
	 * 
	 * @param list
	 * @param inputByConsole
	 * @return void
	 * @Description help user to use this system
	 */
	public void parseCmdForHelp(DoublyList<Customer> list,String inputByConsole){
		String input = inputByConsole;
		if(input.length() == 1){
			System.out.println(Constant.getHelpString());
		}
		else{
			char p = input.charAt(2);
			switch(p){
			case 'A': 
				System.out.println(Constant.getHelpAdd());
				break;
			case 'D':
				System.out.println(Constant.getHelpDelete());
				break;
			case 'U':
				System.out.println(Constant.getHelpUpdate());
				break;
			case 'O': 
				System.out.println(Constant.getHelpSort());
				break;
			case 'L':
				System.out.println(Constant.getHelpLook());
				break;
			case 'Q':
				System.out.println(Constant.getHelpQuit());
				break;
			default:
				;
			}
		}
	}
	
	/**
	 * 
	 * @param list
	 * @param inputByConsole
	 * @return void
	 * @Description show user all customer's message which is sorted
	 */
	public void parseCmdForSort(DoublyList<Customer> list, String inputByConsole){
		String input = inputByConsole;
		String[] buf = input.split(" ");
		String byNameOrId = buf[1];
		
		Customer[]  customers = new Customer[list.nodeNum];
		
		for(int i = 0; i < list.nodeNum; i++){
			Node<Customer> currentNode = list.findNodeByIndex(i);
			customers[i] = currentNode.value;
		}
		
		if(byNameOrId.equals("name")){
			sortNameTool(customers);
		}else{
			sortIdTool(customers);
		}
	}
	
	/**
	 * 
	 * @param a
	 * @return void
	 * @Description called by parseCmdForSort
	 */
	private void sortIdTool(Customer[] a){
		for(int i = 0; i< a.length - 1; i++){
			for(int j = 0; j < a.length - 1 - i; j++){
				if(a[j].getId() > a[j+1].getId()){
					Customer tmp = a[j+1];
					a[j+1] = a[j];
					a[j] = tmp;
				}
			}
		}
		for(int k = 0; k < a.length; k++){
			System.out.println(a[k].getId() + " " + a[k].getName() + " " + a[k].getSex() + " " + 
					a[k].getBirthday() + " " + a[k].getAddress() + " " + a[k].getCall());
		}
	}
	
	/**
	 * 
	 * @param a
	 * @return void
	 * @Description called by parseCmdForSort
	 */
	private void sortNameTool(Customer[] a){
		System.out.println("sortNameTool----------------test");
		for(int i = 0; i< a.length - 1; i++){
			for(int j = 0; j < a.length - 2 - i; j++){
				if(a[j].getName().compareTo(a[j+1].getName()) > 0){
					Customer tmp = a[j+1];
					a[j+1] = a[j];
					a[j] = tmp;
				}
			}
		}
		for(int k = 0; k < a.length; k++){
			System.out.println(a[k].getId() + " " + a[k].getName() + " " + a[k].getSex() + " " + 
					a[k].getBirthday() + " " + a[k].getAddress() + " " + a[k].getCall());
		}
	}
	
	private void printAllcustomer(Customer customer){
			System.out.println(customer.getId() + " " + customer.getName() + " " + customer.getSex() + " " + 
					customer.getBirthday() + " " + customer.getAddress() + " " + customer.getCall());
	}
}
